/**
 * tarside-ftp -- A simple FTP client, using the ANSI C and GTK+.
 * Copyright (C) 2011  degui.chen <degui.chen@qq.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "list.h"
#include "login.h"
static void list_comment_result(char* buffer) {
	printf("start dir result todo... : %s", buffer);
}
static list_comment_hander result_hander = &list_comment_result;
void set_list_result_hander(list_comment_hander hander) {
	result_hander = hander;
}

static void recv_lines(int df_connect) {
	char* result = (char*) malloc(1024 * 10 * sizeof(char));
	char buffer[1024];
	int length;
	int result_lenth = 0;
	while (1) {
		length = recv(df_connect, buffer, 1024, 0);
		if (1 > length) {
			break;
		}
		memmove(result + result_lenth, buffer, length);
		result_lenth += length;
	}
	result[result_lenth] = '\0';
	result_hander(result);
	free(result);
}
static void port_dir_info(int port) {
	char message[256] = { '\0' };
	int bind_socket = open_bind(port);
	int address_length = sizeof(struct sockaddr_in);
	struct sockaddr_in address_client;
	int df_connect;
	sprintf(message, "wait connect %i \n", port);
	set_commond_info(message);
	if ((df_connect = accept(bind_socket, (struct sockaddr *) &address_client, &address_length)) == -1) {
		perror("accept() error\n");
		exit(1);
	}
	sprintf(message, "You got a connection from %s\n", inet_ntoa(address_client.sin_addr));
	set_commond_info(message);
	recv_lines(df_connect);
	sprintf(message, "close connect %i \n", df_connect);
	set_commond_info(message);
	close(df_connect);
	sprintf(message, "close connect %i \n", bind_socket);
	set_commond_info(message);
	close(bind_socket);
}

static void pasv_dir_info(int socket_connect, int port) {
	char message[256] = { '\0' };
	int new_fd = create_connect(get_remote_ip(socket_connect), port);
	recv_lines(new_fd);
	sprintf(message, "close connect %i \n", new_fd);
	set_commond_info(message);
	close(new_fd);
}

void list_comment(int socket_connect, char* list) {
	char message[256] = { '\0' };
	if (get_ftp_model()) {
		char ports[256] = { '\0' };
		create_port_comment(&ports);
		send_cmd(socket_connect, ports);
		send_cmd(socket_connect, list);
		int port = get_port();
		port_dir_info(port);
	} else {//pasv
		char pasv[256] = "PASV \r\n";
		send_cmd(socket_connect, pasv);
		char buffer[1024];
		recv_cmd(socket_connect, buffer, 1024);
		send_cmd(socket_connect, list);
		int port = get_pasv_port(buffer);
		pasv_dir_info(socket_connect, port);
	}
	char buffer[1024] = { '\0' };
	recv_result(socket_connect, "226", buffer);
	set_commond_info(buffer);
}
